package com.javagroup.chessonline.chessman;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class King extends Chessman{
	private static King instanceA = new King(new ImageIcon("Resources/Images/King-icon.png"),0);
	//private static King instanceC = new King(rescaleImage(new File("Resources/Images/King-icon.png"), 56, 56),0);
	private static King instanceB = new King(new ImageIcon("Resources/Images/King-blue-icon.png"),1);
		public static King getInstanceTeamA(){
			return instanceA;
		}
		public static King getInstanceTeamB(){
			return instanceB;
		}
	public King(ImageIcon icon, int team) {
		super(icon, team);
		// TODO Auto-generated constructor stub
	}

	@Override
	public ArrayList<String> canMove(Chessman[] arrayChess) {
		ArrayList<String> moves = new ArrayList<String>();
		String temp = null;
		Chessman c = null;
			if(this.col+1 < 8){
				c = arrayChess[this.row * 8 + this.col + 1];
				if( c != null){ // co can duong
					if(!this.isSameTeam(c)){ // cung team thi khong cho qua
						temp = this.cols[this.col+1]+this.rows[this.row];
						moves.add(temp);
					}
				}
				else{ // khong co can duong
					temp = this.cols[this.col+1]+this.rows[this.row];
					moves.add(temp);
				}
			}
			
			if(this.col-1 >= 0){
			c = arrayChess[this.row * 8 + this.col - 1];
			if( c != null){ // co can duong
				if(!this.isSameTeam(c)){ // cung team thi khong cho qua
					temp = this.cols[this.col-1]+this.rows[this.row];
					moves.add(temp);
				}
			}
			else{ // khong co can duong
				temp = this.cols[this.col-1]+this.rows[this.row];
				moves.add(temp);
			}
			}
			
			if(this.row-1 >= 0){
			c = arrayChess[(this.row - 1) * 8 + this.col ];
			if( c != null){ // co can duong
				if(!this.isSameTeam(c)){ // cung team thi khong cho qua
					temp = this.cols[this.col]+this.rows[this.row-1];
					moves.add(temp);
				}
			}
			else{ // khong co can duong
				temp = this.cols[this.col]+this.rows[this.row-1];
				moves.add(temp);
			}
			}
			if(this.row+1 < 8){
			c = arrayChess[(this.row+1) * 8 + this.col ];
			if( c != null){ // co can duong
				if(!this.isSameTeam(c)){ // cung team thi khong cho qua
					temp = this.cols[this.col]+this.rows[this.row+1];
					moves.add(temp);
				}
			}
			else{ // khong co can duong
				temp = this.cols[this.col]+this.rows[this.row+1];
				moves.add(temp);
			}
			}
			return moves;
	}
	
	public static ImageIcon rescaleImage(File source,int maxHeight, int maxWidth)
	{
	    int newHeight = 0, newWidth = 0;        // Variables for the new height and width
	    int priorHeight = 0, priorWidth = 0;
	    BufferedImage image = null;
	    ImageIcon sizeImage;

	    try {
	            image = ImageIO.read(source);        // get the image
	    } catch (Exception e) {

	            e.printStackTrace();
	            System.out.println("Picture upload attempted & failed");
	    }

	    sizeImage = new ImageIcon(image);

	    if(sizeImage != null)
	    {
	        priorHeight = sizeImage.getIconHeight(); 
	        priorWidth = sizeImage.getIconWidth();
	    }

	    // Calculate the correct new height and width
	    if((float)priorHeight/(float)priorWidth > (float)maxHeight/(float)maxWidth)
	    {
	        newHeight = maxHeight;
	        newWidth = (int)(((float)priorWidth/(float)priorHeight)*(float)newHeight);
	    }
	    else 
	    {
	        newWidth = maxWidth;
	        newHeight = (int)(((float)priorHeight/(float)priorWidth)*(float)newWidth);
	    }


	    // Resize the image

	    // 1. Create a new Buffered Image and Graphic2D object
	    BufferedImage resizedImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
	    Graphics2D g2 = resizedImg.createGraphics();

	    // 2. Use the Graphic object to draw a new image to the image in the buffer
	    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    g2.drawImage(image, 0, 0, newWidth, newHeight, null);
	    g2.dispose();

	    // 3. Convert the buffered image into an ImageIcon for return
	    return (new ImageIcon(resizedImg));
	}

}
